package com.fdb.efp.console.api.rest;

import java.util.List;
import java.util.Map;

import com.fdb.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fdb.efp.console.service.facade.SUserOrgService;
import com.fdb.efp.console.service.vo.SUserOrgVO;
import com.fdb.efp.console.service.facade.SRoleDataRuleService;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class SUserOrgController extends AbstractController  {

	@Autowired
	@Qualifier("sUserOrgService")
	private SUserOrgService sUserOrgService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 列表数据查询
	 */
	@RequestMapping(value="/s/user/orgs", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SUserOrgVO>> querySUserOrgAll(
			@RequestBody SUserOrgVO vo) {
		// 根据用户角色获取用户权限方案
		List<SUserOrgVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
			case Owner:
				outputVo = sUserOrgService.queryAllOwner(vo);
				break;
			case CurrOrg:
				outputVo = sUserOrgService.queryAllCurrOrg(vo);
				break;
			case CurrDownOrg:
				outputVo = sUserOrgService.queryAllCurrDownOrg(vo);
				break;
			default:
				outputVo = sUserOrgService.queryAllOwner(vo);
				break;
		}
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键查询详情
	 */
	@RequestMapping(value="/s/user/org/{userCode}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<SUserOrgVO> queryByPk(@PathVariable("userCode") String userCode) {
		SUserOrgVO inVo = new SUserOrgVO();
		inVo.setUserCode(userCode);
		SUserOrgVO outputVo = sUserOrgService.queryByPk(inVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键删除信息
	 */
	@RequestMapping(value="/s/user/org", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SUserOrgVO inSUserOrgVo) {
		int outputVo = sUserOrgService.deleteByPk(inSUserOrgVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 */
	@RequestMapping(value="/s/user/org", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SUserOrgVO inSUserOrgVo) {
		int outputVo = sUserOrgService.updateByPk(inSUserOrgVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 */
	@RequestMapping(value="/s/user/org", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSUserOrg(@RequestBody SUserOrgVO inSUserOrgVo) {
		int outputVo = sUserOrgService.insertSUserOrg(inSUserOrgVo);
		return getResponseData(outputVo);

	}
}
